﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Deflection
{
    class SubEnemies : ParticleSystem
    {
        public bool DoneSpawning { get; set; }
        public int Size { get; set; }
        public bool Finished { get; set; }

        public SubEnemies(Vector2 position, Texture2D texture, Color color, int size)  : base(position, texture, color, 0.3333f)
        {
            Size = size;
            DoneSpawning = false;
            Finished = false;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }


        public override void SpawnParticles(GameTime gameTime)
        {
            // enemy sprite = 24x24
            // sub enemies = 3x3 (9)
            // 24 / 3 = 8
            // 8 pixels per sub enemy
            int xy = Texture.Width;
            
            int subEnemySpacing = (int)xy / Size;

            if (HasFreeParticles() && !DoneSpawning)
            {
                for (int x = 0; x < Size; x++)
                {
                    for (int y = 0; y < Size; y++)
                    {
                        Vector2 particlePostion = (Position + new Vector2(x * subEnemySpacing, y * subEnemySpacing)) - new Vector2(subEnemySpacing, subEnemySpacing);
                        float aThird = 1f / 3f;
                        Particle p = SpawnParticle(particlePostion, Color, aThird);
                        p.Fade = true;
                        p.MaxLife = 45;
                        if (this.Color == Color.GhostWhite)
                        {
                            p.FadeRate = 1.0f;
                        }
                        else
                        {
                            p.FadeRate = 0.9f;
                        }

                        // Make the enemy explode outwards
                        Random rand = new Random(x * y + gameTime.TotalGameTime.Milliseconds);

                        float r = (float)rand.NextDouble();
                        r = r * rand.Next(-5, 6);


                        Vector2 explodingVelocity = Position - particlePostion;

                        explodingVelocity.Normalize();
                        explodingVelocity = Vector2.Multiply(explodingVelocity, GameParameters.BASE_ENEMY_SPEED * 1.5f);
                        explodingVelocity = Vector2.Add(explodingVelocity, new Vector2(r, r));
                        p.Velocity = -explodingVelocity;


                        Particles.Add(p);                        
                    }
                }
                
            }
            else
            {
                //Console.WriteLine("P: " + Particles.Count);
                DoneSpawning = true;
                if (Particles.All(particle => (particle.Life >= particle.MaxLife)))
                {
                    Finished = true;
                }
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            //spriteBatch.Draw(TestEnemy2.EnemySprite, Position, null, Color.Green, 0, new Vector2(TestEnemy2.EnemySprite.Width / 2, TestEnemy2.EnemySprite.Height / 2), 0.25f, SpriteEffects.None, 0);
            base.Draw(gameTime, spriteBatch);
        }
    }
}
